/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#include "carina/graphics-engine.hh"
#include "carina/common/system.hh"

namespace Carina
{
GraphicsEngine::GraphicsEngine(const string& _icon, unsigned flags)
{
    // Loads the file.
    if(!m_Settings.load())
    {
        system( + "/carina_settings");
        if(!m_Settings.load())
            THROW_EXCEPTION("unable to load the settings file. Please, run the settings manager manually or file a bug report.");
    }

    string log_file = m_Settings.getLogFile();
    if(!log_file.empty())
        Log::getSingleton().setLogFile(log_file);
    Log::getSingleton().setMinLoggingLevel(m_Settings.getLogLevel());

    string renderer_name = m_Settings.getRenderer();
    if(!renderer_name.empty())
    {
        m_RenderingLibrary = m_RenderingSystem.getLibrary(renderer_name);
        if(!m_RenderingLibrary)
            THROW_EXCEPTION("the application has failed to open " + m_Settings.getRenderer() + " library. Please, check your settings and drivers.");
    }
    else
    {
        if(!m_RenderingSystem.getLibraryCount())
            THROW_EXCEPTION("no rendering back-end libraries were found");
        m_RenderingLibrary = m_RenderingSystem.getLibrary(0);
        assert(m_RenderingLibrary);
    }

    CE_GET_ATTRIBUTE(ResourceCache) = make_aligned_shared<ResourceCache>();
    for(size_t i = 0; i < m_Settings.getResourceCount(); ++i)
        CE_GET_ATTRIBUTE(ResourceCache)->addResourcePath(m_Settings.getResource(i));
    string path_icon = _icon.empty() ? string() : CE_GET_ATTRIBUTE(ResourceCache)->getResource(_icon);
    m_WindowManager = make_aligned_shared<WindowManager>(_icon);

	if(flags & CE_GRAPHICS_LOAD_CONTEXTS)
		for(size_t i = 0; i < m_Settings.getContextCount(); ++i)
			CE_GET_ATTRIBUTE(Contexts).push_back(make_aligned_shared<GraphicsEngineContext>(*this, m_Settings.getContext(i)));
}

void GraphicsEngine::updateContexts()
{
    for(auto& ctx : CE_GET_ATTRIBUTE(Contexts))
        ctx->updateContext();
}

GraphicsEngineContext::GraphicsEngineContext(GraphicsEngine& cge, ContextSettings& ctx_settings)
	:   m_Window(cge.createWindow(WindowDescription(ctx_settings.getName(), ctx_settings.getWidth(), ctx_settings.getHeight(), ctx_settings.getMSAA()))),
        m_ResMgr(make_aligned_shared<ResourceManager>(m_Window->getRenderer(), cge.getResourceCache())),
        m_LightingMgr(make_aligned_shared<LightingManager>(m_ResMgr)),
        m_WorldMgr(make_aligned_shared<WorldManager>()),
        m_NodeMgr(make_aligned_shared<NodeManager>(m_WorldMgr, m_LightingMgr))
{
}

void GraphicsEngineContext::updateContext()
{
    m_ResMgr->updateResources();
}
}
